<html>
<head><meta charset="utf-8"><title>Memory layout of nested dyn trait objects? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20nested.20dyn.20trait.20objects.3F.html">Memory layout of nested dyn trait objects?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242635660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20nested%20dyn%20trait%20objects%3F/near/242635660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexa VanHattum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20nested.20dyn.20trait.20objects.3F.html#242635660">(Jun 14 2021 at 17:52)</a>:</h4>
<p>I'm having trouble understanding the memory layout when you have nested references to dynamic trait objects. In particular, if the backing data to a dyn trait object is itself a fat pointer, is the 2nd half of that fat pointer a reference to another vtable?</p>
<p>This is in the context of the a custom Rust backend in the <a href="https://github.com/model-checking/rmc">RMC</a> project, where our goal is to have a bit-precise match to rustc.</p>
<p>The high level code I'm trying to understand:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">log</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">Write</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Send</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">sink</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">dest</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">Write</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nb">Send</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">log</span><span class="p">.</span><span class="n">as_mut</span><span class="p">());</span><span class="w"></span>
</code></pre></div>
<p>The second half of the fat pointer for <code>dest</code> points to a vtable that indicates a size of <code>16</code>. What is the interpretation of those 16 bytes? I though it should be another fat pointer to the dyn trait object<code>log</code>, where the first 8 bytes are log's data and the next 8 are another vtable, this one for <code>log</code>. However, trying to read that nested vtable, I get back garbage instead of reasonable size and align values.</p>
<p>Thanks so much!</p>
<p>Playground with the full example/code to access the vtable <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=105b8e53abbc4e8ae4eae263baee85fe">here</a>.</p>



<a name="242636430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20nested%20dyn%20trait%20objects%3F/near/242636430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20nested.20dyn.20trait.20objects.3F.html#242636430">(Jun 14 2021 at 17:58)</a>:</h4>
<p>I haven't verified, but I think <code>dest</code>'s vtable will be <code>Write for &amp;mut W</code> with <code>W = dyn Write</code>, and <code>dest</code>'s pointer will be the heap allocation of 16 bytes -- then _that_ will be a pointer and vtable for <code>Sink</code> itself</p>



<a name="242636549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20nested%20dyn%20trait%20objects%3F/near/242636549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20nested.20dyn.20trait.20objects.3F.html#242636549">(Jun 14 2021 at 17:59)</a>:</h4>
<p>/me looks at the playground...</p>



<a name="242636850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Memory%20layout%20of%20nested%20dyn%20trait%20objects%3F/near/242636850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alexa VanHattum <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Memory.20layout.20of.20nested.20dyn.20trait.20objects.3F.html#242636850">(Jun 14 2021 at 18:01)</a>:</h4>
<p>Ah, I think my interpretation of the second nested vtable was right, but I was getting garbage results because I had a <code>*mut ()</code> on line 52 where I needed a <code>*mut usize</code>. </p>
<p>This <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=547ea1a5268b73febaef712b1deac4ff">edit on the playground</a> seems to correspond with this interpretation; the inner vtable has size 0 and align 1, which seem reasonable.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>